package com.help.help.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.help.help.entity.AddressEntity;
import com.help.help.util.ExceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.help.help.entity.UserEntity;
import com.help.help.service.UserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author 小🌤
 * @email 1578442339@qq.com
 * @date 2021-01-07 15:41:11
 */
@RestController
@RequestMapping("/help/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询全部的用户
     */
     @GetMapping("/findAll")
     public R findAll(){
         return R.ok().put("data",userService.list());
     }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 导出
     * @param response
     * @param ids
     * @throws IOException
     */
    @RequestMapping("/updaloa")
    public void   upload(HttpServletResponse response, Integer[] ids) throws IOException {
        List<UserEntity> list = userService.listByIds(Arrays.asList(ids));
        ExceUtil.out("用户信息导出", response);
        EasyExcel.write(response.getOutputStream(), UserEntity.class).sheet("用户信息导出信息").doWrite(list);
    }
}
